// pages/circleMember/circleMember.js
import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { jump } from "../../utils/utils";

Page({

  /**
   * 页面的初始数据
   */
  data: {
     circleId: '', //圈子id
     info: {}, //详情数据
     auth: false, //是否有成员操作权限 
     isOwner: false, //是否是圈主
     page: 1,
     total: 0, //总页数
     num: 0,//成员人数
     isDone: false,
     list: [], //圈子列表数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      circleId: options.circleId
    },()=>{
      this.getInfo()
      this.getList(1)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  // 获取详情数据
  async getInfo() {
      const { circleId } = this.data
       if (!circleId) return
       const res = await request.get(api.circleDetail, { id: circleId }, { loading: false })
       this.setData({
         info: {
           ...res?.data,
         }
       })
       this.setData({
        auth: this.data.info.owner && this.data.info.userEdit===0,
        isOwner: this.data.info.owner
       })
   },
  /**
   * 获取列表
   * @param {*} page 当前页
   */
  async getList(page){
   const res=await request.get(api.circleMember,{
       id: this.data.circleId,
       page,
       size: 10
    }, {loading: false})
   if(res.httpStatus==200){
    this.setData({
      list: page === 1 ? res?.data?.records : [
        ...this.data.list,
        ...res?.data?.records,
      ],
      total: res?.data?.pages,
      num: res?.data?.total
    })
   }
    wx.stopPullDownRefresh()
  },
  //移除成员
  async remove(e){
     let params={
       circleId: this.data.circleId,
       id: e.currentTarget.dataset.id
     }
     let res= await request.post(api.circleRemoveUser,params, {loading: false})
     if(res.httpStatus == 200){
       let list= this.data.list
       list.splice(e.currentTarget.dataset.index, 1);
       this.setData({
        list,
        num: this.data.num-1
       })
       wx.showToast({
         title: '移除成功',
         icon: 'none'
       })
     }
  },
  //跳转到成员邀请页面
  jumpInvite(){
     jump('circleMemberInvite', { circleId: this.data.circleId, isOwner: this.data.isOwner})
  },
  //跳转到用户主页
  jumpHome(e){
     if(e.currentTarget.dataset?.id){
      jump('home', { id: e.currentTarget.dataset.id})
     }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   this.setData({
      page: 1,
      isDone: false,
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, total } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})